<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@page import="java.text.SimpleDateFormat" %>
<%@page import="java.text.DateFormat" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
    <head>
        <base href="<%=basePath%>"/>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>新闻中国</title>
        <link href="css/main.css" rel="stylesheet" type="text/css"/>
        <script type="text/javascript" src="./js/jquery-1.12.4.min.js"></script>
        <script type="text/javascript">
            function check() {
                var login_username = document.getElementById("uname");
                var login_password = document.getElementById("upwd");
                if (login_username.value == "") {
                    alert("用户名不能为空！请重新填入！");
                    login_username.focus();
                    return false;
                } else if (login_password.value == "") {
                    alert("密码不能为空！请重新填入！");
                    login_password.focus();
                    return false;
                }
                return true;
            }

            function focusOnLogin() {
                var login_username = document.getElementById("uname");
                login_username.focus();
            }

        </script>
    </head>

    <body onload="">

        <div id="header">
            <c:if test="${empty sessionScope.user}">
                <div id="top_login">
                    <form action="<%=basePath%>users/doLogin" method="post" onsubmit="return check()">
                        <input type="hidden" name="opr" value="login"/>
                        <label for="uname"> 登录名 </label>
                        <input type="text" id="uname" name="uname" value="${username}" class="login_input"/>
                        <label for="upwd"> 密&#160;&#160;码 </label>
                        <input type="password" id="upwd" name="upwd" value="${password}" class="login_input"/>
                        <input type="submit" class="login_sub" value="登录"/>
                        <label id="error">${loginError}</label>
                        <img src="images/friend_logo.gif" alt="Google" id="friend_logo"/>
                    </form>
                </div>
            </c:if>
            <c:if test="${!empty sessionScope.user}">
                <div id="top_login">
                    <span>欢迎：${sessionScope.user.uname}</span>
                    &nbsp;&nbsp;&nbsp;&nbsp;
                    <a href="<%=basePath%>users/logout">退出登录</a>
                    <c:if test="${sessionScope.user.urole==2}">
                        &nbsp;&nbsp;&nbsp;&nbsp;<a href="<%=basePath%>users/admin">进入后台</a>
                    </c:if>
                </div>
            </c:if>

            <div id="nav">
                <div id="logo"><img src="images/logo.jpg" alt="新闻中国"/></div>
                <div id="a_b01"><img src="images/a_b01.gif" alt=""/></div>
                <!--mainnav end-->
            </div>
        </div>
        <div id="container">

            <%@ include file="index-elements/index_sidebar.jsp" %>

            <div class="main">
                <div class="class_type"><img src="images/class_type.gif" alt="新闻中心"/></div>
                <div class="content">
                    <ul class="class_date">
                        <c:forEach items="${topics}" var="topic" varStatus="i">
                            <c:if test="${i.count % 11 == 1}"><li id='class_month'></c:if>
                            <a style="color: ${topic.tid==tid?'red':'#335884'}"
                               href="<%=basePath%>news?tid=${topic.tid}"><b>${topic.tname}</b></a>
                            <c:if test="${i.count % 11 == 0}"></li></c:if>
                            <c:set var="n" value="${i.count}"/>
                        </c:forEach>
                        <c:if test="${n % 11 != 0}"></li></c:if>
                    </ul>
                    <ul class="classlist">
                        <c:choose>
                            <c:when test="${news == null}"><h6>出现错误，请稍后再试或与管理员联系</h6></c:when>
                            <c:when test="${empty news.list}"><h6>抱歉，没有找到相关的新闻</h6></c:when>
                            <c:otherwise>
                                <c:forEach items="${news.list}" var="item" varStatus="i">
                                    <li>
                                        <a href='<%=basePath%>news/details?nid=${item.nid}'>${item.ntitle}</a>
                                        <span><fmt:formatDate value="${item.ncreatedate}"
                                                              pattern="yyyy-MM-dd HH:mm:ss"/></span>
                                    </li>
                                    <c:if test="${i.count % 5 == 0}">
                                        <li class='space'></li>
                                    </c:if>
                                </c:forEach>
                            </c:otherwise>
                        </c:choose>
                        <p align="center"> 当前页数:[${news.pageIndex}/${news.totalPages}]&nbsp;
                            <c:if test="${news.pageIndex > 1}">
                                <a href="<%=basePath%>news?pageIndex=1&tid=${tid}">首页</a>&nbsp;
                                <a href="<%=basePath%>news?pageIndex=${news.pageIndex - 1}&tid=${tid}">上一页</a>
                            </c:if>
                            <c:if test="${news.pageIndex < news.totalPages}">
                                <a href="<%=basePath%>news?pageIndex=${news.pageIndex + 1}&tid=${tid}">下一页</a>
                                <a href="<%=basePath%>news?pageIndex=${news.totalPages}&tid=${tid}">末页</a>
                            </c:if>
                        </p>
                    </ul>
                </div>
                <%@ include file="index-elements/index_rightbar.html" %>
            </div>
        </div>
<%@ include file="index-elements/index_bottom.html" %>
